package cn.qhuan.modules.service.impl;

import cn.qhuan.modules.entity.Artist;
import cn.qhuan.modules.mapper.ArtistMapper;
import cn.qhuan.modules.constos.PageResponseResult;
import cn.qhuan.modules.dtos.ExhiDto;
import cn.qhuan.modules.dtos.ExhiTimeDto;
import cn.qhuan.modules.entity.Exhibition;
import cn.qhuan.modules.mapper.ExhibitionMapper;
import cn.qhuan.modules.service.IExhibitionService;
import cn.qhuan.modules.entity.Works;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description: 展厅表
 * @Author: 司翔宇
 * @Date: 2020-12-18
 * @Version: V1.0
 */
@Service
public class ExhibitionServiceImpl extends ServiceImpl<ExhibitionMapper, Exhibition> implements IExhibitionService {

    @Autowired
    private ExhibitionMapper exhibitionMapper;

    @Autowired
    private ArtistMapper artistMapper;



    @Override
    public Result<?> findByKeyWord(ExhiDto exhiDto) {
        //1.检查参数
        if (exhiDto == null) {
            return Result.error("缺少参数");
        }
        //进行分页检查
        exhiDto.checkParam();
        //进行条件分页查询
        IPage pageParam = new Page(exhiDto.getPage(), exhiDto.getSize());
        LambdaQueryWrapper<Exhibition> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(exhiDto.getKeyWord())) {
            lambdaQueryWrapper.like(Exhibition::getExhiName, exhiDto.getKeyWord());

        }

        IPage pageList = page(pageParam, lambdaQueryWrapper);
        //封装结果进行返回
        PageResponseResult responseResult = new PageResponseResult(exhiDto.getPage(), exhiDto.getSize(), (int) pageList.getTotal());
        responseResult.setResult(pageList.getRecords());

        return responseResult;
    }

    @Override
    public List<String> findExhiIdsByClassId(String id) {
        return exhibitionMapper.findExhiIdsByClassId(id);
    }

    @Override
    public Result findExhiByTime(ExhiTimeDto dto) {


        //1.检查参数
        if (dto == null) {
            return Result.error("缺少参数");
        }
        //分页检查
        dto.checkParam();
        //根据条件分页
        LambdaQueryWrapper<Exhibition> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Exhibition> list = exhibitionMapper.selectList(lambdaQueryWrapper);
        List<Exhibition> qhList=new ArrayList<>();

        for (Exhibition qhExhi : list) {
            Map<String, String> map = stringToMaP(qhExhi.getId());
            Date beginDate = ExhiTimeDto.getDate(map.get("展览时间").split("-")[0]);
            Date endDate = ExhiTimeDto.getDate(map.get("展览时间").split("-")[1]);
            Date nowdate = ExhiTimeDto.getDate(ExhiTimeDto.getNowTime());
            Long beginTime = ExhiTimeDto.getTime(map.get("展览时间").split("-")[0]);
            Long nowTime = ExhiTimeDto.getTime(ExhiTimeDto.getNowTime());
            long result =nowTime- beginTime;
            if ((beginDate.before(nowdate)||result==0)&&endDate.after(nowdate)){
               qhList.add(qhExhi);
           }
        }
        return Result.OK(qhList);


    }

    @Override
    public Result<?> findEndExhiByTime(ExhiTimeDto dto) {
        //1.检查参数
        if (dto == null) {
            return Result.error("缺少参数");
        }
        //分页检查
        dto.checkParam();

        LambdaQueryWrapper<Exhibition> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Exhibition> list = exhibitionMapper.selectList(lambdaQueryWrapper);
        List<Exhibition> qhList=new ArrayList<>();

        for (Exhibition qhExhi : list) {
            Map<String, String> map = stringToMaP(qhExhi.getId());
            Date endDate = ExhiTimeDto.getDate(map.get("展览时间").split("-")[1]);
            Date nowDate = ExhiTimeDto.getDate(ExhiTimeDto.getNowTime());
            if (endDate.before(nowDate)){
                qhList.add(qhExhi);
            }
        }
        return Result.OK(qhList);
    }

    @Override
    public Result<?> findNoStartedByTime(ExhiTimeDto dto) {
        //1.检查参数
        if (dto == null) {
            return Result.error("缺少参数");
        }
        //分页检查
        dto.checkParam();

        LambdaQueryWrapper<Exhibition> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Exhibition> list = exhibitionMapper.selectList(lambdaQueryWrapper);
        List<Exhibition> qhList=new ArrayList<>();

        for (Exhibition qhExhi : list) {
            Map<String, String> map = stringToMaP(qhExhi.getId());
            Date beginDate = ExhiTimeDto.getDate(map.get("展览时间").split("-")[0]);
            Date nowDate = ExhiTimeDto.getDate(ExhiTimeDto.getNowTime());
            if (beginDate.after(nowDate)){
                qhList.add(qhExhi);
            }
        }
        return Result.OK(qhList);
    }

    @Override
    public Result<?> loadNewExhiList(ExhiTimeDto dto) {
        //1.检查参数
        if (dto == null) {
            return Result.error("缺少参数");
        }
        //分页检查
        dto.checkParam();
        LambdaQueryWrapper<Exhibition> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Exhibition> list = exhibitionMapper.selectList(lambdaQueryWrapper);
        List<Exhibition> qhList=new ArrayList<>();

        for (Exhibition qhExhi : list) {
            Map<String, String> map = stringToMaP(qhExhi.getId());
            Date beginDate = ExhiTimeDto.getDate(map.get("展览时间").split("-")[0]);
            Date endDate = ExhiTimeDto.getDate(map.get("展览时间").split("-")[1]);
            Date nowDate = ExhiTimeDto.getDate(ExhiTimeDto.getNowTime());
            int begin = beginDate.compareTo(nowDate);
            int end =endDate.compareTo(nowDate);
            if (begin <=0 && end >0){
               qhList.add(qhExhi);
               Collections.sort(qhList, new Comparator<Exhibition>() {
                   @Override
                   public int compare(Exhibition o1, Exhibition o2) {
                       Map<String, String> map1 = stringToMaP(o1.getId());
                       Map<String, String> map2 = stringToMaP(o2.getId());
                       String begin1 = map1.get("展览时间").split("-")[0];
                       String begin2 = map2.get("展览时间").split("-")[0];
                       Long begin1time = ExhiTimeDto.getTime(begin1);
                       Long begin2time = ExhiTimeDto.getTime(begin2);
                       Long nowtime = ExhiTimeDto.getTime(ExhiTimeDto.getNowTime());
                       long result = (nowtime - begin1time) - (nowtime - begin2time);
                       return (int) result;
                   }
               });
            }
        }
        return Result.OK(qhList);
    }

    @Override
    public Result selectWorksCountByExhiId(String exhiId) {
        //1.检查参数
        if (exhiId == null ){
            return Result.error("缺少参数");
        }
        Integer count = exhibitionMapper.selectWorksCountByExhiId(exhiId);
        return Result.OK(count);
    }

    @Override
    public Result<?> selectDocCountByExhiId(String exhiId) {
        //1.检查参数
        if (exhiId == null ){
            return Result.error("缺少参数");
        }
        Integer count = exhibitionMapper.selectDocCountByExhiId(exhiId);
        return Result.OK(count);
    }


    @Override
    public Result<?> queryExhiByArtistId(String artistId) {
        //1.参数判断
        if (artistId == null ){
            return Result.error("缺少参数");
        }
        //2.根据艺术家id 获取艺术家对象
        Artist artist = artistMapper.selectById(artistId);
        LambdaQueryWrapper<Exhibition> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Exhibition> list = exhibitionMapper.selectList(lambdaQueryWrapper);
        List<Exhibition> qhList =new ArrayList<>();
        for (Exhibition qhExhi : list) {
            Map<String, String> map = stringToMaP(qhExhi.getId());
            String values = map.get("作者");
            if (artist.getName().equals(values)){
                qhList.add(qhExhi);
                Collections.sort(qhList, new Comparator<Exhibition>() {
                    @Override
                    public int compare(Exhibition o1, Exhibition o2) {
                        Map<String, String> map1 = stringToMaP(o1.getId());
                        Map<String, String> map2 = stringToMaP(o2.getId());
                        String begin1 = map1.get("展览时间").split("-")[0];
                        String begin2 = map2.get("展览时间").split("-")[0];
                        Long begin1time = ExhiTimeDto.getTime(begin1);
                        Long begin2time = ExhiTimeDto.getTime(begin2);
                        Long nowtime = ExhiTimeDto.getTime(ExhiTimeDto.getNowTime());
                        long result = (nowtime - begin2time)-(nowtime - begin1time)  ;
                        long result1 = (begin2time - nowtime)-(begin1time - nowtime)  ;
                        return (int) result1;
                    }
                });
            }
        }
        return Result.OK(qhList);
    }


    @Override
    public Result<?> findWorksListByExhiId(ExhiDto dto) {
        //检查参数
        if (dto.getExhiId() == null){
            return Result.error("缺少参数");
        }
        //设置分页条件
        dto.setPage((dto.getPage()-1)*dto.getSize());

        List<Works> worksList = exhibitionMapper.findWorksListByExhiId(dto);
        //统计数据
        int count =exhibitionMapper.findWorksCountByExhiId(dto);
        //封装结果进行返回
        PageResponseResult pageResponseResult =new PageResponseResult(dto.getPage(),dto.getSize(),count);
        pageResponseResult.setResult(worksList);
        return pageResponseResult;
    }

    /**
     * 根据传过来的展厅id，把自定义字段转换为map
     * @param id
     * @return
     */
    public  Map<String,String> stringToMaP(String id){

        Exhibition qhExhi = exhibitionMapper.selectById(id);
        String customField = qhExhi.getCustomField();
        String replace = customField.replace("[", "")
                .replace("]","").replace("{","")
                .replace("}","")
                .replace("\"","")
                .replace(" ","");

        Map<String,String> hash =new HashMap<>();
        String[] split = replace.split(",");
        for (int i   =  0; i < split.length; i++) {
            if (i % 2 == 0){
                hash.put(split[i].split(":")[1],split[i+1].split(":")[1]);
            }
        }
        return hash;
    }
}
